//
//  ADView.m
//  UITableView_Cell定制
//
//  Created by LZXuan on 14-12-18.
//  Copyright (c) 2014年 LZXuan. All rights reserved.
//

#import "ADView.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "NewsModel.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
@implementation ADView
{
    UIScrollView *_scrollView;
    UIPageControl *_pageControl;
    AFHTTPRequestOperationManager *_manager;
    NSMutableArray *_dataArr;
    NSMutableArray *_titleArr;
}
- (id)initWithFrame:(CGRect)frame url:(NSString *)url
{
    self = [super initWithFrame:frame];
    if (self) {
        self.Url=url;
        [self creatView];
    }
    return self;
}
//下载
-(void)downloadData{
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[[AFCompoundResponseSerializer alloc]init];
    _dataArr=[[NSMutableArray alloc]init];
    _titleArr=[[NSMutableArray alloc]init];
    
    //__weak typeof (self)weakSelf=self;
    [_dataArr removeAllObjects];
   [_manager GET:self.Url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
       if (responseObject) {
           NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
           NSArray *listArr=dict[@"list"];
           //i：限制头视图的个数
           int i=0;
           for (NSDictionary *itemDict in listArr) {
               NewsModel *model=[[NewsModel alloc]init];
               [model setValuesForKeysWithDictionary:itemDict];
               [_dataArr addObject:model];
               if (i==4) {
                   break;
               }
           }
           
           
           
           
           
       }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)creatView {
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 160)];
    for (int i = 0; i < 4; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenSize.width*i, 0, kScreenSize.width, 160)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.png",i]];
        [_scrollView addSubview:imageView];
           }
    //下面设置滚动视图
    _scrollView.contentSize = CGSizeMake(4*kScreenSize.width, 160);
    _scrollView.showsVerticalScrollIndicator = NO;
    //按页
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    
    [self addSubview:_scrollView];
    //页码器
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 160-30, kScreenSize.width, 30)];
    _pageControl.numberOfPages = 4;
    
    [_pageControl addTarget:self action:@selector(pageClick:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_pageControl];
}
- (void)pageClick:(UIPageControl *)page {
    //修改滚动视图的偏移量
    [_scrollView setContentOffset:CGPointMake(_scrollView.bounds.size.width*page.currentPage, 0) animated:YES];
}
//减速停止的时候
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //修改页码
    CGPoint offset = _scrollView.contentOffset;
    _pageControl.currentPage = offset.x/_scrollView.bounds.size.width;
}

@end
